package com.daxia.bumall.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.UserStatisticsDTO;
import com.daxia.bumall.util.DateUtils;
@Repository
public class UserStatisticsDAO extends GenericDAOHibernate<UserStatisticsDTO> {
	/**
	 * 统计该天登录用户
	 * @param date
	 * @return
	 */
	public UserStatisticsDTO statisticsUserByDate(Date date){
		StringBuffer sql=new StringBuffer();
		List<Object> paras=new ArrayList<Object>();
		sql.append("select  count(0) loginAmount from `user` ");
		sql.append("where DATE_FORMAT(lastLoginTime, '%Y-%m-%d')=?");
		String today=DateUtils.format(date, "yyyy-MM-dd");
		paras.add(today);
		return (UserStatisticsDTO)super.findBySQL(sql.toString(), paras.toArray(), UserStatisticsDTO.class).get(0);
	}
	/**
	 * 统计一段时间的登录用户
	 * @return
	 */
	public UserStatisticsDTO statisticsUserByDate(Date startTime,Date endTime){
		if(null==startTime)
			return null;
		StringBuffer sql=new StringBuffer();
		List<Object> paras=new ArrayList<Object>();
		sql.append("select  count(0) loginAmount from `user` ");
		sql.append(" where DATE_FORMAT(lastLoginTime, '%Y-%m-%d')>=? ");
		String start=DateUtils.format(startTime, "yyyy-MM-dd");
		paras.add(start);
		if(null!=endTime){
			sql.append(" and DATE_FORMAT(lastLoginTime, '%Y-%m-%d')<? ");
			String end=DateUtils.format(endTime, "yyyy-MM-dd");
			paras.add(end);
		}
		return (UserStatisticsDTO)super.findBySQL(sql.toString(), paras.toArray(), UserStatisticsDTO.class).get(0);
		
	}
}
